const imgUrl = 'https://mystore-1304299581.cos.ap-guangzhou.myqcloud.com/static/image/'
import api from '@/common/request/index.js'
const state = {
	//商品首页分类
	category: [
		{
			cate_name: '默认',
			selected: false
		}, 
		{
			cate_name: '兑换量',
			selected: false
		},
		{
			cate_name: '距离远近',
			selected: false,
		}
	],
	//商品首页菜单
	menu: [
	],
	goods_list: [
		
	],
	goods_lists: [],
	tabs: [{
		id: 0,
		label: "你可能还喜欢",
	},{
		id: 1,
		label: "24小时热销",
	}],
	love: [],
    tabs_group: [],
    // 商品详情
    detail:{},
    // 商品分类列表
    goodCategory: [],
    // 商品分类
    giftPage: [],
    // 获取商品分类信息标题id
    title: '',
    // 获取商品详情
    goodsInfo: {},
    // 拼团产品
    group_goods:[],
    // 拼团产品
    group_goods:[],
    // 拼团列表
    groupList: [],
    // 拼团ID
    groupKey: '',
	// 销量
	saleNum: 0,
	goodsLove: [], // 猜你喜欢
}
const mutations = {
	set_detail(state, payload) {
		state.detail= payload
	},
	set_giftPage(state, payload) {
		state.giftPage= payload
	},
	set_title(state, payload) {
		state.title = payload
	},
	set_goodsInfo(state, payload) {
	    const res = JSON.parse(payload.detail_html);
	    const detailImg = [...new Set(res)];
	    state.goodsInfo = payload;
	    state.goodsInfo.detail_html = detailImg;
	}
}
const actions = {
	get_detail({commit}, payload) {
		commit('set_detail', payload)
	},
	get_giftPage({commit}, payload) {
		commit('set_giftPage', payload)
	},
	get_title({commit}, payload) {
		commit('set_title', payload)
	},
	// 分页商品查询
	get_gift({commit, state}, payload) {
		  return new Promise((resolve, reject) => {
			  api('gift', payload).then(res => {
				  state.gift = res.data
			  })
		  })
	},
	// 商品详情查询
	get_goodsInfo({commit, state}, payload) {
		commit('set_goodsInfo', payload)
	},
	// 查询人气拼团
	get_group_goods({commit, state}, payload) {
		  return new Promise((resolve, reject) => {
			  api('giftpage', payload).then(res => {
				  state.group_goods = res.data.data
			  })
		  })
	},
	// 商品首页的商品信息
	get_goods({commit, state}, payload) {
		return new Promise((resolve, reject) => {
			api('giftpage', payload).then(res => {
				resolve(res.data.data);
			})
		})
	},
	// 商品首页的商品信息
	get_goodsLove({commit, state}, payload) {
		return new Promise((resolve, reject) => {
			api('giftpage', payload).then(res => {
				state.goodsLove = res.data.data;
				resolve(res.data.data);
			})
		})
	},
	// 发起团购
	get_createGroup({commit, state}, payload) {
		return new Promise((resolve, reject) => {
			api('createGroup', payload).then(res => {
				
			})
		})
	},
	get_groupList({commit, state}, payload) {
		return new Promise((resolve, reject) => {
			api('groupList', payload).then(res => {
				state.groupList = res.data;
				resolve(res.data);
			})
		})
	},
	get_categoryS({commit, state}, payload) {
	    state.menu = payload
		state.menu.sort((a,b)=>{
		 return a.id - b.id;
		})
	},
    // 设置拼团id
    get_groupKey({commit, state}, payload) {
        state.groupKey = payload;
    },
    // 设置拼团id
    get_saleNum({commit, state}, payload) {
        state.saleNum = payload;
    },
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
